package com.yl.mytest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.util.Log;

public class MainActivity2 extends Activity {
    private String tag = "MainActivity2";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
//        run1();
//        run2(5);
        run3(5);

    }


    void run1() {
        SerialExecutorExample serialExecutorExample = new SerialExecutorExample();
        for (int i = 0; i < 5; i++) {
            Log.e(tag, "================" + i + "============");

            Runnable task = () -> {
                ThreadTest threadTest = new ThreadTest();
                threadTest.start();
            };
            serialExecutorExample.executeTask(task);
        }
        serialExecutorExample.shutdown();
    }

    void run2(int count) {
        ThreadTest threadTest0 = null;
        ThreadTest threadTest1 = null;
        ThreadTest threadTest2 = null;
        ThreadTest threadTest3 = null;
        ThreadTest threadTest4 = null;
        Log.e("-------------", count + "");
        switch (count) {
            case 1:
                threadTest0 = new ThreadTest();
                threadTest0.start();
                break;
            case 2:
                threadTest0 = new ThreadTest();
                threadTest1 = new ThreadTest();
                try {
                    threadTest0.start();
                    threadTest0.join();
                    threadTest1.start();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            case 3:
                threadTest0 = new ThreadTest();
                threadTest1 = new ThreadTest();
                threadTest2 = new ThreadTest();
                try {
                    threadTest0.start();
                    threadTest0.join();
                    threadTest1.start();
                    threadTest1.join();
                    threadTest2.start();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            case 4:
                threadTest0 = new ThreadTest();
                threadTest1 = new ThreadTest();
                threadTest2 = new ThreadTest();
                threadTest3 = new ThreadTest();
                try {
                    threadTest0.start();
                    threadTest0.join();
                    threadTest1.start();
                    threadTest1.join();
                    threadTest2.start();
                    threadTest2.join();
                    threadTest3.start();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            case 5:
                threadTest0 = new ThreadTest();
                threadTest1 = new ThreadTest();
                threadTest2 = new ThreadTest();
                threadTest3 = new ThreadTest();
                threadTest4 = new ThreadTest();
                try {
                    threadTest0.start();
                    threadTest0.join();
                    threadTest1.start();
                    threadTest1.join();
                    threadTest2.start();
                    threadTest2.join();
                    threadTest3.start();
                    threadTest3.join();
                    threadTest4.start();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    ThreadTest[] threadTest = new ThreadTest[5];

    void run3(int count) {
        for (int i = 0; i < count; i++) {
            Log.e(tag, "================" + i + "============");
            threadTest[i] = new ThreadTest();
            try {
                threadTest[i].start();
                threadTest[i].join();
                if (i == count - 2) {
                    threadTest[i + 1] = new ThreadTest();
                    threadTest[i + 1].start();
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
